<script setup>
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { getOverviewApi } from "@/api/report";
import { ElMessage } from "element-plus";
import {
  User,
  Opportunity,
  Document,
  Setting,
  DataAnalysis,
  Management,
} from "@element-plus/icons-vue";

const router = useRouter();

// 概览数据
const overviewData = ref({
  clueTotal: 0,
  clueWaitAllot: 0,
  clueWaitFollow: 0,
  clueFollowing: 0,
  clueFalse: 0,
  clueConvertBusiness: 0,
  businessTotal: 0,
  businessWaitAllot: 0,
  businessWaitFollow: 0,
  businessFollowing: 0,
  businessFalse: 0,
  businessConvertCustomer: 0,
});

// 获取概览数据
const getOverview = async () => {
  try {
    const result = await getOverviewApi();
    if (result.code === 1) {
      overviewData.value = result.data;
    } else {
      ElMessage.error(result.msg || "获取概览数据失败");
    }
  } catch (error) {
    console.error("获取概览数据失败:", error);
    ElMessage.error("获取概览数据失败，请稍后重试");
  }
};

// 快捷操作跳转
const handleQuickAction = (route) => {
  router.push(route);
};

onMounted(() => {
  getOverview();
});
</script>

<template>
  <div class="home-container">
    <!-- 欢迎标题 -->
    <div class="welcome-header">
      <h1>欢迎您, 进入轻客管家系统</h1>
    </div>

    <!-- 概览区域 -->
    <div class="overview-section">
      <!-- 线索概览 -->
      <div class="overview-card">
        <h2 class="overview-title">线索概览</h2>
        <div class="overview-grid">
          <div class="overview-item">
            <div class="overview-number">{{ overviewData.clueTotal }}</div>
            <div class="overview-label">总线索数</div>
          </div>
          <div class="overview-item">
            <div class="overview-number">{{ overviewData.clueWaitAllot }}</div>
            <div class="overview-label">待分配线索数</div>
          </div>
          <div class="overview-item">
            <div class="overview-number">{{ overviewData.clueWaitFollow }}</div>
            <div class="overview-label">待跟进线索数</div>
          </div>
          <div class="overview-item">
            <div class="overview-number">{{ overviewData.clueFollowing }}</div>
            <div class="overview-label">跟进中线索数</div>
          </div>
          <div class="overview-item">
            <div class="overview-number">{{ overviewData.clueFalse }}</div>
            <div class="overview-label">伪线索数</div>
          </div>
          <div class="overview-item">
            <div class="overview-number">
              {{ overviewData.clueConvertBusiness }}
            </div>
            <div class="overview-label">转商机线索数</div>
          </div>
        </div>
      </div>

      <!-- 商机概览 -->
      <div class="overview-card">
        <h2 class="overview-title">商机概览</h2>
        <div class="overview-grid">
          <div class="overview-item">
            <div class="overview-number">{{ overviewData.businessTotal }}</div>
            <div class="overview-label">总商机数</div>
          </div>
          <div class="overview-item">
            <div class="overview-number">
              {{ overviewData.businessWaitAllot }}
            </div>
            <div class="overview-label">待分配商机数</div>
          </div>
          <div class="overview-item">
            <div class="overview-number">
              {{ overviewData.businessWaitFollow }}
            </div>
            <div class="overview-label">待跟进商机数</div>
          </div>
          <div class="overview-item">
            <div class="overview-number">
              {{ overviewData.businessFollowing }}
            </div>
            <div class="overview-label">跟进中商机数</div>
          </div>
          <div class="overview-item">
            <div class="overview-number">{{ overviewData.businessFalse }}</div>
            <div class="overview-label">回收商机数</div>
          </div>
          <div class="overview-item">
            <div class="overview-number">
              {{ overviewData.businessConvertCustomer }}
            </div>
            <div class="overview-label">转客户商机数</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 快捷操作区域 -->
    <div class="quick-actions">
      <div class="action-button" @click="handleQuickAction('/resource/course')">
        <el-icon class="action-icon"><Document /></el-icon>
        <div class="action-content">
          <div class="action-title">课程管理</div>
          <div class="action-subtitle">课程信息维护</div>
        </div>
      </div>

      <div
        class="action-button"
        @click="handleQuickAction('/resource/activity')"
      >
        <el-icon class="action-icon"><Management /></el-icon>
        <div class="action-content">
          <div class="action-title">活动管理</div>
          <div class="action-subtitle">营销活动管理</div>
        </div>
      </div>

      <div class="action-button" @click="handleQuickAction('/clue/list')">
        <el-icon class="action-icon"><User /></el-icon>
        <div class="action-content">
          <div class="action-title">线索管理</div>
          <div class="action-subtitle">潜在客户追踪</div>
        </div>
      </div>

      <div class="action-button" @click="handleQuickAction('/business/list')">
        <el-icon class="action-icon"><Opportunity /></el-icon>
        <div class="action-content">
          <div class="action-title">商机管理</div>
          <div class="action-subtitle">销售机会管理</div>
        </div>
      </div>

      <div class="action-button" @click="handleQuickAction('/customer')">
        <el-icon class="action-icon"><Setting /></el-icon>
        <div class="action-content">
          <div class="action-title">客户管理</div>
          <div class="action-subtitle">客户信息维护</div>
        </div>
      </div>

      <div class="action-button" @click="handleQuickAction('/system/log')">
        <el-icon class="action-icon"><DataAnalysis /></el-icon>
        <div class="action-content">
          <div class="action-title">日志统计</div>
          <div class="action-subtitle">系统操作日志</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home-container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.welcome-header {
  text-align: center;
  margin-bottom: 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 30px 20px;
  border-radius: 15px;
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);
  position: relative;
  overflow: hidden;
}

.welcome-header::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    45deg,
    transparent 30%,
    rgba(255, 255, 255, 0.1) 50%,
    transparent 70%
  );
  animation: shine 3s ease-in-out infinite;
}

.welcome-header h1 {
  color: #fff;
  font-size: 32px;
  font-weight: 600;
  margin: 0;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  letter-spacing: 2px;
  position: relative;
  z-index: 1;
}

@keyframes shine {
  0% {
    transform: translateX(-100%);
  }
  50% {
    transform: translateX(100%);
  }
  100% {
    transform: translateX(-100%);
  }
}

.overview-section {
  display: flex;
  gap: 30px;
  margin-bottom: 40px;
}

/* 玻璃质感卡片，使用主题变量做左右卡片配色 */
.overview-card {
  --accent: #3b82f6; /* 默认蓝色（线索） */
  --accent-2: #60a5fa;
  --accent-veil: rgba(59, 130, 246, 0.06);

  flex: 1;
  background: linear-gradient(180deg, #ffffff, #fbfbff);
  border: 1px solid rgba(0, 0, 0, 0.06);
  border-radius: 14px;
  padding: 26px;
  box-shadow: 0 10px 30px rgba(31, 66, 135, 0.08);
  position: relative;
  overflow: hidden;
}

/* 右侧卡片（商机）改为紫色系 */
.overview-card:nth-of-type(2) {
  --accent: #8b5cf6;
  --accent-2: #a78bfa;
  --accent-veil: rgba(139, 92, 246, 0.06);
}

/* 卡片内部高光 */
.overview-card::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(
    120% 120% at 0% 0%,
    var(--accent-veil),
    transparent 60%
  );
  pointer-events: none;
}

.overview-title {
  font-size: 20px;
  font-weight: 700;
  margin: 0 0 20px 0;
  text-align: center;
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  letter-spacing: 1px;
}

.overview-title::after {
  content: "";
  display: block;
  width: 72px;
  height: 3px;
  margin: 10px auto 0;
  border-radius: 3px;
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
  opacity: 0.85;
}

.overview-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 18px;
}

.overview-item {
  position: relative;
  text-align: center;
  padding: 18px 16px;
  background: linear-gradient(180deg, #ffffff, var(--accent-veil));
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, 0.06);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: transform 0.25s ease, box-shadow 0.25s ease,
    border-color 0.25s ease;
}

.overview-item::after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
  border-top-left-radius: 12px;
  border-top-right-radius: 12px;
  opacity: 0.8;
}

.overview-item:hover {
  transform: translateY(-4px);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.08), 0 4px 10px rgba(0, 0, 0, 0.05);
  border-color: var(--accent-2);
}

.overview-number {
  font-size: 32px;
  font-weight: 800;
  margin-bottom: 8px;
  background: linear-gradient(180deg, var(--accent-2), var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.overview-label {
  font-size: 12px;
  color: #5f6b7a;
  line-height: 1.4;
}

.quick-actions {
  display: flex;
  gap: 25px;
  justify-content: center;
  padding: 20px;
}

.action-button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 150px;
  height: 130px;
  background: linear-gradient(145deg, #ffffff, #f8f9fa);
  border: 2px solid #e8eaed;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  position: relative;
  overflow: hidden;
}

.action-button::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    135deg,
    rgba(64, 158, 255, 0.05) 0%,
    rgba(64, 158, 255, 0.1) 100%
  );
  opacity: 0;
  transition: opacity 0.3s ease;
}

.action-button:hover {
  transform: translateY(-8px) scale(1.02);
  box-shadow: 0 12px 40px rgba(64, 158, 255, 0.25);
  border-color: #409eff;
  background: linear-gradient(145deg, #ffffff, #f0f8ff);
}

.action-button:hover::before {
  opacity: 1;
}

.action-button:active {
  transform: translateY(-4px) scale(1.01);
}

.action-icon {
  font-size: 36px;
  color: #409eff;
  margin-bottom: 15px;
  transition: all 0.3s ease;
  filter: drop-shadow(0 2px 4px rgba(64, 158, 255, 0.2));
}

.action-button:hover .action-icon {
  transform: scale(1.1);
  filter: drop-shadow(0 4px 8px rgba(64, 158, 255, 0.3));
}

.action-content {
  text-align: center;
  position: relative;
  z-index: 1;
}

.action-title {
  font-size: 17px;
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 6px;
  transition: color 0.3s ease;
}

.action-button:hover .action-title {
  color: #409eff;
}

.action-subtitle {
  font-size: 12px;
  color: #7f8c8d;
  line-height: 1.4;
  transition: color 0.3s ease;
}

.action-button:hover .action-subtitle {
  color: #5a6c7d;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .overview-section {
    flex-direction: column;
  }

  .quick-actions {
    flex-wrap: wrap;
    gap: 15px;
  }

  .action-button {
    width: 120px;
    height: 100px;
  }
}

@media (max-width: 768px) {
  .overview-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .quick-actions {
    flex-direction: column;
    align-items: center;
  }

  .action-button {
    width: 200px;
    height: 80px;
    flex-direction: row;
    gap: 15px;
  }

  .action-icon {
    margin-bottom: 0;
  }
}
</style>
